<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input v-model="params.poleName" clearable placeholder="请输入一体杆名称" class="search-main" />
      <span class="search-label">一体杆编号：</span>
      <el-input v-model="params.poleNumber" clearable placeholder="请输入一体杆编号" class="search-main" />
      <span class="search-label">运行状态：</span>
      <el-select v-model="params.poleStatus" placeholder="请选择运行状态">
        <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="searchFn">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button v-auth-dir="`pole:info:add_edit`" type="primary" @click="addRodFn">添加一体杆</el-button>
      <el-dialog
        title="添加区域"
        :visible="dialogVisible"
        width="580px"
        @close="closeDialog"
      >
        <!-- 表单接口 -->
        <div class="form-container">
          <el-form ref="addForm" :model="addForm" :rules="addFormRules">
            <el-form-item label="一体杆名称" prop="poleName">
              <el-input v-model="addForm.poleName" placeholder="请填写一体杆名称" />
            </el-form-item>
            <el-form-item label="一体杆编号" prop="poleNumber">
              <el-input v-model="addForm.poleNumber" placeholder="请填写一体杆编号" />
            </el-form-item>
            <el-form-item label="一体杆IP" prop="poleIp">
              <el-input v-model="addForm.poleIp" placeholder="请填写一体杆IP" />
            </el-form-item>
            <el-form-item label="关联区域" prop="areaId">
              <el-select v-model="addForm.areaId" placeholder="请选择关联区域">
                <el-option
                  v-for="item in areaDropList"
                  :key="item.areaId"
                  :label="item.areaName"
                  :value="item.areaId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="一体杆类型" prop="poleType">
              <el-select v-model="addForm.poleType" placeholder="请选择一体杆类型">
                <el-option
                  v-for="item in poleType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
        </template>
      </el-dialog>
      <el-button v-auth-dir="`pole:info:remove`" @click="dels">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div v-auth-dir="`pole:info:list`" class="table">
      <el-table style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="一体杆IP" prop="poleIp" />
        <el-table-column label="安装区域" prop="areaName" />
        <el-table-column label="一体杆类型" prop="poleType" :formatter="formatterTypeFn" />
        <el-table-column label="运行状态" prop="poleStatus" :formatter="formatterStatusFn" />
        <el-table-column label="操作" fixed="right" width="180">
          <!-- scope的值：{row：一行数据对象} -->
          <template #default="scope">
            <el-button v-auth-dir="`pole:info:add_edit`" size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <auth-btn btn-perm="pole:info:remove">
              <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button></auth-btn>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="currentChangeFn"
      />
    </div>
  </div>
</template>
<script>
import { addRodAPI, delRodAPI, editRodAPI, getAreaDropListAPI, getRodListAPI } from '@/apis/rod'

export default {
  data() {
    return {
      params: {
        'pageSize': 10, // 一页条数
        'page': 1, // 页码
        'poleName': '', // 一体杆名称
        'poleNumber': '', // 一体杆编号
        'poleStatus': null// 一体杆状态
      },
      statusList: [
        { label: '全部',
          value: null
        }, {
          label: '正常',
          value: 0
        }, {
          label: '异常',
          value: 1
        }
      ],
      tableData: [], // 列表数据
      total: 0, // 总数
      selectedCarList: [], // 被选择的列表
      dialogVisible: false, // 弹窗可视
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: null,
        poleType: ''
      },
      areaDropList: [], // 下拉列表
      poleType: [{ label: '入口', value: 'entrance' }, { label: '出口', value: 'export' }], // 添加窗口类型下拉列表
      addFormRules: {
        poleName: [
          { required: true, message: '请输入一体杆名称', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '请输入一体杆编号', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入一体杆IP', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '请选择关联区域', trigger: 'blur' }
        ],
        poleType: [
          { required: true, message: '请选择关联区域', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getAreaDropList()
  },
  methods: {
    edit(row) {
      // console.log(row)

      // 1. 打开弹框
      this.dialogVisible = true
      // 2. 解构必要字段
      const { id, poleName, poleNumber, poleIp, areaId, poleType } = row
      this.addForm = {
        id, poleName, poleNumber, poleIp, areaId, poleType
      }
    },
    // 弹窗确认事件
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.addForm.id) {
            await editRodAPI(this.addForm)
            // console.log(res)
            this.$message.success('编辑成功')
          } else {
            // 1. 提交接口
            await addRodAPI(this.addForm)
            this.$message.success('添加成功')
          }
          // 2. 获取列表
          this.getList()
          // 3. 关闭弹框
          this.dialogVisible = false
          this.addForm = {
            poleName: '',
            poleNumber: '',
            poleIp: '',
            areaId: null,
            poleType: ''
          }
          // 5.跳转到第一页
          this.params.page = 1
        }
      })
    },
    // 获取下拉列表方法
    async getAreaDropList() {
      const res = await getAreaDropListAPI()
      // console.log(res)
      this.areaDropList = res.data
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
    },
    // 添加编辑一体杆事件
    addRodFn() {
      this.dialogVisible = true
    },
    // 搜索事件
    searchFn() {
      this.getList()
      this.params.page = 1
    },
    // 格式化状态
    formatterStatusFn(row) {
      const obj = {
        0: '正常',
        1: '异常'
      }
      return obj[row.poleStatus]
    },
    // 格式化类型
    formatterTypeFn(row) {
      // console.log(row)
      const obj = {
        'entrance': '入口',
        'export': '出口',
        'center': '中心'
      }
      return obj[row.poleType]
    },
    // 改变页码
    currentChangeFn(page) {
      this.params.page = page
      this.getList()
    },
    // 得到列表
    async getList() {
      const res = await getRodListAPI(this.params)
      // console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 表格选项变化时
    handleSelectionChange(val) {
      // val 是选中的这些行的数据对象的数组集合
      this.selectedCarList = val
    },
    // 批量删除
    async dels() {
      const ids = this.selectedCarList.map(obj => obj.id)
      await delRodAPI(ids.join(','))
      this.getList()
      this.$message.success('批量删除成功')
    },
    // 删除
    del(id) {
      this.$confirm('确认删除该合同吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRodAPI(id)
        if (res.code === 50000 && res.msg === '演示数据不允许操作!') {
          this.$message.error('演示数据不允许操作')
        } else {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
  button{
    margin-right: 20px;
  }
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}
::v-deep .form-container{
  padding: 5px 100px;
  .el-form-item__label{
    display: block; /* 将标签显示为块级元素，占据整行 */
    margin-bottom: 5px; /* 标签和输入框之间的间距 */
  }
  .el-form-item__content{
    .el-select{
      width:100%
    }
  }
}
</style>
